package com.example.demo5;
/**
 * @author 张成
 * @date 2022/3/22 10:39
 * @version 1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

@WebServlet(name = "GuessingServlet", value = "/GuessingServlet")
public class GuessingServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession(true);;
        int guess;
        int input = 0;
        String username = request.getParameter("username");
        try {
            input = Integer.parseInt(username);
            guess = (int) session.getAttribute("Guess");
            if (guess <= 9 || guess >= 100) {
                throw new Exception();
            }
        } catch (Exception e) {
            e.printStackTrace();
            guess = new Random().nextInt(89) + 10;

            session.setAttribute("Guess", guess);
            session.setAttribute("count",0);
            ArrayList<Integer> action = new ArrayList<>();
            session.setAttribute("list",action);
            response.getWriter().write("我想出一个10-99之间的数，请你猜！");
            response.getWriter().write("<form name=\"reg\" action=\"GuessingServlet\" method=\"post\">\n" +
                    "    <input name=\"username\" type=\"number\"/><input type=\"submit\" value=\"提交\" id=\"bt\"/>\n" +
                    "</form>");
            return;
        }

        int count = (int) session.getAttribute("count");
        ArrayList<Integer> action  = (ArrayList<Integer>) session.getAttribute("list");
        System.out.println(input + " " + guess);
        action.add(input);
        count++;

        if (input == guess) {
            response.getWriter().write(action +"\n");
            response.getWriter().write("总共猜了" + count + "次\n" +
                    "祝贺你，答对了!");
            response.getWriter().write("<a href=\"GuessingServlet\">再猜一次.</a>\n");
        } else if (input < guess) {
            response.getWriter().write("太小了!请重猜!\n");
        } else {
            response.getWriter().write("太大了!请重猜!\n");
        }
        try {
            session.setAttribute("count",count);
            session.setAttribute("action",action);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.getWriter().write("<form name=\"reg\" action=\"GuessingServlet\" method=\"post\">\n" +
                "    <input name=\"username\" type=\"number\"/><input type=\"submit\" value=\"提交\" id=\"bt\"/>\n" +
                "</form>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
